export const INVALUE_VALUE_LIST = [null, undefined, NaN, Infinity, "null", "undefined", "NaN", "Infinity"];

/** 入参是不允许是 null, undefined, NaN，以及无意义的字符串（但可以是空字符串）*/
export function isValid(mess: any): boolean {
  return !INVALUE_VALUE_LIST.includes(String(mess).replace(/\s+/g, ""));
}

export function isNotValid(mess: any): boolean {
  return !isValid(mess);
}

export function isStringValid(mess: any): boolean {
  return toString(mess).length > 0;
}

export function isStringNotValid(mess: any): boolean {
  return toString(mess).length === 0;
}

export function isObject(mess: any): boolean {
  if (isNotValid(mess)) return false;
  return !["number", "string", "boolean"].includes(typeof mess);
}

// ======================= 基础类型转换 =======================
// ======================= 基础类型转换 =======================
// ======================= 基础类型转换 =======================
// ======================= 基础类型转换 =======================
// ======================= 基础类型转换 =======================

/** 转换成数字，最差最差的情况就是返回 0
 * @expect Number(object) => NaN => 0
 * @expect Number(true) => 1 => 0
 * @wanner 如果 mess 不正确则返回这个期望 wanner 值
 * @expect Math.ceil 向前取整 如 1.2=>2, -1.2=>-1
 * @expect Math.round 四舍五入取整
 * @expect Math.floor 向后取整 如 1.2=>1, -1.2=>-2
 */
export function toNumber(mess: any, wanner?: number): number {
  const wan = { value: Number(wanner) };
  if (Number.isNaN(wan.value) || !Number.isFinite(wan.value)) {
    wan.value = 0;
  }

  if (isValid(mess)) {
    const ok = { value: Number(mess) };
    const isNotOK = Number.isNaN(ok.value) || !Number.isFinite(ok.value);
    return isNotOK ? wan.value : ok.value;
  } else {
    return wan.value;
  }
}

/** 转换成字符串
 * @expect 对象会变成 [object Object]
 * @wanner 如果 mess 不正确则会返回这个期望的值
 */
export function toString(mess: any, wanner?: string): string {
  const wan = { value: String(wanner) };
  if (isNotValid(wan.value)) wan.value = "";

  const ok = { value: String(mess) };
  return isNotValid(ok.value) ? wan.value : ok.value;
}

/** 转换成 true\false */
export function toBoolean(mess: any): boolean {
  if (isValid(mess)) {
    const isFalseStr = String(mess).replace(/\s+/g, "") === "false";
    return isFalseStr ? false : Boolean(mess);
  } else {
    return false;
  }
}

export function doBigIntSort(mess: bigint, range: bigint[]) {
  const values = [mess, ...range];
  const list = values.sort((first, next) => {
    if (first > next) return -1; // 返回负数：按照 first next 摆放
    else if (first < next) return 1; // 返回正数：按照 next first 摆放
    else return 0; // 相等，保持原顺序
  });
  return {
    min: list[list.length - 1],
    max: list[0],
  };
}
